package com.qzimp.fujiancuo.controller;
import com.qzimp.fujiancuo.entity.LikeAllVo;
import com.qzimp.fujiancuo.entity.PersonageAndLegendVO;
import com.qzimp.fujiancuo.entity.UserLikes;
import com.qzimp.fujiancuo.entity.Userinfo;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.ResultVo;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.service.PersonageLegendService;
import com.qzimp.fujiancuo.service.UserLikesService;
import com.qzimp.fujiancuo.service.UserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 梁少君
 * @date 2024/12/21
 */
@RestController
@Api(tags = "liangshaojun-controller")
@RequestMapping("/lsj")
public class LiangShaoJunController {
    @Resource
    private PersonageLegendService personageLegendService;
    @Resource
    private UserinfoService userinfoService;

    @PostMapping("/add")
    @ApiOperation("添加人物的故事接口")
    public ResultVo addPersonageLegend(@RequestParam Integer personageId, @RequestParam Integer legendId) {
        // 添加人物与传说的关联
        personageLegendService.add(personageId, legendId);
        return new ResultVo(ResultCode.ADD_PERSONAGE_LEGEND_SUCCESS);
    }

    // 根据人物 ID 获取对应的传奇故事
    @GetMapping("/personage/{keyWord}")
    @ApiOperation("查询人物的故事接口")
    public ResultVo getLegendsByKeyWord(@PathVariable String keyWord) {
        // 获取人物对应的所有传奇故事
        List<PersonageAndLegendVO> legendsByKeyWord = personageLegendService.getLegendsByKeyWord(keyWord);
        return new ResultVo(ResultCode.SUCCESS,legendsByKeyWord);
    }

    @Resource
    private UserLikesService userLikeService;

    @PostMapping("/like")
    @ApiOperation("点赞古厝")
    public ResultVo likeCuo(@RequestParam Integer userId, @RequestParam Integer cuoId) {
        /*查询用户然后在去查询用户的关联就记录*/
        Userinfo userinfoByUserId = userinfoService.findUserinfoByUserId(userId);
        if (userinfoByUserId == null) {
            throw new ApiException(ResultCode.FAIL);
        }
        //查询userid是否点赞
        UserLikes existingLike  = userLikeService.findByUserIdAndCuoId(userId,cuoId);
        if (existingLike != null) {
            //该user已点赞
            throw new ApiException(ResultCode.USER_LIKE_COU_ERROR);
        }
        // 创建新的点赞记录
        UserLikes userLike = new UserLikes();
        userLike.setUserId(userId);
        userLike.setCuoId(cuoId);
        userLike.setCreatedAt(LocalDateTime.now());
        userLikeService.save(userLike);
        return  new ResultVo(ResultCode.USER_LIKE_COU_SUCCESS,null);
    }

    @PostMapping("/unlike")
    @ApiOperation("取消点赞古厝")
    public ResultVo unlikeCuo(@RequestParam Integer userId, @RequestParam Integer cuoId) {
        /*查询用户然后在去查询用户的关联就记录*/
        Userinfo userinfoByUserId = userinfoService.findUserinfoByUserId(userId);
        if (userinfoByUserId == null) {
            throw new ApiException(ResultCode.FAIL);
        }
        UserLikes existingLike  = userLikeService.findByUserIdAndCuoId(userId,cuoId);
        if (existingLike == null) {
            throw new ApiException(ResultCode.USER_NOT_LIKE_COU_ERROR);
        }
        //若有点赞记录，则把这条点赞记录删除
        userLikeService.removeById(existingLike.getId());
        return  new ResultVo(ResultCode.USER_NOT_LIKE_COU_SUCCESS,null);
    }

    @ApiOperation("获取所有点赞信息")
    @GetMapping("/user/likeAll")
    public ResultVo getAllLikes() {
        List<UserLikes> allLikes = userLikeService.findAll();
        List<LikeAllVo> collect = allLikes.stream().map(item -> {
            LikeAllVo likeAllVo = new LikeAllVo();
            likeAllVo.setUserLikes(item);
            Userinfo userinfo = userinfoService.findUserinfoByUserId(item.getUserId());
            likeAllVo.setLoginName(userinfo.getLoginName());
            likeAllVo.setSalt(userinfo.getSalt());
            likeAllVo.setEmail(userinfo.getEmail());
            return likeAllVo;
        }).collect(Collectors.toList());
        if (collect.isEmpty()) {
            throw new ApiException(ResultCode.NO_DATA_ERROR);
        }
        return new ResultVo(ResultCode.LIKES_FETCH_SUCCESS, collect);
    }

}
